package com.walkline.app;

import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.UiApplication;

import com.walkline.screen.LoginFullScreen;
import com.walkline.screen.WeiboScreen;
import com.walkline.util.Enumerations.ShortcutKey;
import com.walkline.weibo.WeiboSDK;

public class WeiboApp extends UiApplication
{
	private WeiboSDK _weibo;
	private WeiboAppConfig _appConfig;

	public static void main(String[] args)
	{
		if (args != null && args.length > 0)
		{
			if (args[0].equals("startup"))
			{
				String newAppName = WeiboAppConfig.APP_TITLE;
				WeiboAppConfig config = new WeiboAppConfig();
				config.initialize(null);
				int keyIndex = config.getShortcutKey();

				if (keyIndex > 0)
				{
					String shortcut_key = ShortcutKey.choicesShortcutKeys[keyIndex];
					newAppName += "(" + shortcut_key + WeiboAppConfig.UNDERLINE + ")";

					HomeScreen.setName(newAppName);
				}
			} else if (args[0].equals("clean")) {
				WeiboAppConfig config = new WeiboAppConfig();

				config.clean();
			}
    	} else {
    		WeiboApp theApp = new WeiboApp();
    		theApp.enterEventDispatcher();
    	}
	}

	public WeiboApp()
	{
		_weibo = WeiboSDK.getInstance();

		synchronized (Application.getEventLock())
		{
			_appConfig = new WeiboAppConfig();
			_appConfig.initialize(_weibo);
		}

		_weibo.setAppConfig(_appConfig);

		if (_appConfig.isAccountEmpty() && _appConfig.isAutoMode())
		{
			pushScreen(new LoginFullScreen(_weibo));
		} else {
			pushScreen(new WeiboScreen(_weibo));
		}
	}
}